package com.jigu.movie;

import com.jigu.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieCobntroller {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 选择对应的服务
     */
    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @GetMapping("movie/user")
    public User movieUser(){

        User user = restTemplate.getForObject("http://user-service/user/getuser", User.class);
        return user;
    }

    @GetMapping("test")
    public void  test(){

        ServiceInstance serviceInstance = loadBalancerClient.choose("user-service");
        System.out.println(serviceInstance.getServiceId() + ":" + serviceInstance.getHost()+":" + serviceInstance.getPort());

        ServiceInstance serviceInstance1 = loadBalancerClient.choose("user-service1");
        System.out.println(serviceInstance1.getServiceId() + ":" + serviceInstance1.getHost()+":" + serviceInstance.getPort());

    }


}
